//
//  MapSheetView.m
//  i5db
//
//  Created by iMuse on 15-5-28.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "MapSheetView.h"
#import "AppCenter.h"
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

@implementation MapSheetView
{
    UIView * backgroundView;
    UIView * sheetView;
    
    CGFloat  height;
    CGFloat  weight;
}
- (id)initWithFrame:(CGRect)frame :(id) delegate array:(NSMutableArray*) mapArray;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        self.mapArray = mapArray;
        self.delegate = delegate;
        [self initView:frame];
    }
    return self;
}

-(void)removeView
{
    [UIView animateWithDuration:0.5 animations:^(){
        backgroundView.alpha = 0;
        self.alpha = 0;
        
    }completion:^(BOOL finash){
        [self removeFromSuperview];
    }];
    
}
-(void) initView:(CGRect) frame
{
    
    UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
    [self addGestureRecognizer:gesture];
    
    
    
   // NSArray * array = [NSArray arrayWithObjects:@"百度地图",@"腾讯地图",@"显示路线",nil];
    backgroundView = [[UIView alloc] init];
    backgroundView.frame = frame;
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.alpha = 0;
    [self addSubview:backgroundView];
    
    weight = 60;
    height = 30;
    if (isPad)
    {
      height = 50;
      weight = 40;
    }
    
    
    sheetView = [UIView newAutoLayoutView];

    sheetView.backgroundColor = [UIColor whiteColor];
    sheetView.layer.cornerRadius = 8;
    sheetView.frame = CGRectMake(0, 0, frame.size.width/3+weight,height*(self.mapArray.count+1));
    sheetView.center = self.center;
    
    UILabel * addressLabel = [[UILabel alloc] init];
    addressLabel.textColor = [AppCenter color:@"188,188,188,1"];
    addressLabel.frame     = CGRectMake(0, 0, sheetView.frame.size.width, height);
    addressLabel.text = @"导航到 朝阳区 光华路";
    addressLabel.textAlignment = NSTextAlignmentCenter;
    addressLabel.adjustsFontSizeToFitWidth = YES;
    addressLabel.minimumScaleFactor = 0.2;
    [sheetView addSubview:addressLabel];
    
    for (NSInteger i=0; i<self.mapArray.count; i++)
    {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, height+i*height, sheetView.frame.size.width, height);
        
        [button setTitle:[self.mapArray objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
        
        UILabel * line = [[UILabel alloc] init];
        line.backgroundColor = [AppCenter color:@"188,188,188,1"];
        line.frame = CGRectMake(0, 0, sheetView.frame.size.width, 0.5);
           
        [button addSubview:line];
            
        
        [sheetView addSubview:button];
    }
    
    [self addSubview:sheetView];
    
    
}
-(void)startAnimation
{
    [UIView animateWithDuration:0.5 animations:^{
        backgroundView.alpha = 0.1;
        self.alpha = 1;
        
    }completion:^(BOOL finish){
        
    }];
}

-(void)clickButton:(UIButton *) button
{
    
    if ([self.delegate respondsToSelector:@selector(mapSheetButton:)])
    {
        [self.delegate performSelector:@selector(mapSheetButton:) withObject:button];
    }
    if ([button.titleLabel.text isEqualToString:@"显示路线"]||[button.titleLabel.text isEqualToString:@"隐藏路线"])
    {
       // [self removeView];
    }
}

+(UIViewController*)rootController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}

+(void) showMapsArray:(NSMutableArray*) array delegate:(id) delegate
{
    UIViewController * rootController = [self rootController];
    
    MapSheetView * mapSheetView = [[MapSheetView alloc] initWithFrame:rootController.view.bounds :delegate array:array];
    [mapSheetView startAnimation];
    [rootController.view addSubview:mapSheetView];
}

@end
